package com.bops.biz.permission;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class UserGrant {

    private List<Role>         roleList;
    private Map<String, Boolean> menuPermissonMap;//访问的菜单权限缓存

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }
    /**
     * 返回是否能显示该菜单条
     * @param menuBar
     * @param menuItem menuItem=null,表示是否可以显示menuBar
     * @return
     */
    public boolean checkMenu(String menuBarId,String menuItem)
    {
    	if (menuBarId==null)
    	{
    		return false;
    	}
        if (menuPermissonMap == null) {
        	menuPermissonMap = new HashMap<String, Boolean>();
            for (Role role : roleList) {
                for (String menu : role.getAllMenuList()) {
                    if (menu != null) {
                    	int index = menu.indexOf("|");
                    	if (index!=-1)
                    	{
                        	String bar = menu.substring(0,index);
                        	menuPermissonMap.put(bar,true);
                        	menuPermissonMap.put(menu,true);	                            	
                    	}
                    }
                }
            }
        }
    	String key = menuBarId+((menuItem==null)?"":"|"+menuItem);
    	Boolean value = menuPermissonMap.get(key);
    	if (value==null)
    	{
    		menuPermissonMap.put(key,false);
    		return false;
    	}
    	return value.booleanValue();
    }


    /**
     * 返回userName对于url是否有访问权限
     * 
     * @param userName
     * @param url
     * @param request
     * @return
     */
    public boolean checkUserGrant(String url, HttpServletRequest request) {
        if (roleList == null) {
            return false;
        }
        Map<String, Grant> grantMap = new HashMap<String, Grant>();
        for (Role role : roleList) {
            List<String> allGrantList = role.getAllGrangList();
            for (String grantText :allGrantList) {
                if (grantText != null) {
                    if (grantText.equals(url)) //如果碰到相同的URL就认为是合法的
                    {
                        return true; 
                    }
                    grantMap.put(grantText, GrantManager.getGrant(grantText));
                }
            }
        }
        //下面处理带有简单参数的URL访问权限
        Iterator<Grant> it = grantMap.values().iterator();
        while (it.hasNext()) {
            Grant tmpGrant = it.next();
            if (tmpGrant != null) {
                if (tmpGrant.checkUserGrant(url, request)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    /**
     * 判断是否有权限
     */
    public boolean hasPermission(String roleId)
    {
    	if (roleId==null || roleList==null || roleList.size()==0)
    	{
    		return false;
    	}
    	for (Role tmp:roleList)
    	{
    		if (tmp!=null && tmp.hasPermission(roleId))
    		{
    			return true;
    		}
    	}
    	return false;
    }
}
